package co.laomag.coffee.utils;

import co.laomag.coffee.model.vo.AdMenuVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 马泽朋
 * @version 1.0
 * @date 2020/4/22 下午 5:18
 */
public class CreatTree {
    /**
     *
     * @param list
     * @return
     */
    public static List<AdMenuVo> listToTree(List<AdMenuVo> list) {
        //用递归找子。
        List<AdMenuVo> treeList = new ArrayList<AdMenuVo>();
        for (AdMenuVo tree : list) {
            if (tree.getParentid() == 0) {
                treeList.add(findChildren(tree, list));
            }
        }
        return treeList;
    }

    private static AdMenuVo findChildren(AdMenuVo tree, List<AdMenuVo> list) {
        for (AdMenuVo node : list) {
            if (node.getParentid().equals(tree.getMenuId())) {
                if (tree.getChildren() == null) {
                    tree.setChildren(new ArrayList<AdMenuVo>());
                }
                tree.getChildren().add(findChildren(node, list));
            }
        }
        return tree;
    }
}
